#pragma once

#include "HashTable.h"

namespace ltw
{
    template<class K,class V,class Hash = HashFunc<K>>
    class unordered_map
    {
        struct MapKeyOfT
        {
            const K& operator()(const pair<K,V>& kv)
            {
                return kv.first;
            }
        };
    public:
        typedef typename HashTable<K,pair<const K,V>,MapKeyOfT,Hash>::Iterator iterator;
        typedef typename HashTable<K,pair<const K,V>,MapKeyOfT,Hash>::Const_Iterator const_iterator;

        iterator begin()
        {
            return _ht.Begin();
        }

        iterator end()
        {
            return _ht.End();
        }

        const_iterator begin()const
        {
            return _ht.Begin();
        }

        const_iterator end()const
        {
            return _ht.End();
        }

        pair<iterator,bool> insert(const pair<K,V>& kv)
        {
            return _ht.Insert(kv);
        }

        V& operator[](const K& key)
        {
            pair<iterator,bool> ret = _ht.Insert({key,V()});
            return ret.first->second;
        }

        iterator Find(const K& key)
        {
            return _ht.Find(key);
        }
        bool Erase(const K& key)
        {
            return _ht.Erase(key);
        }

    private:
        HashTable<K,pair<const K,V>,MapKeyOfT,Hash> _ht;
    };

    void test_map()
	{
		unordered_map<string, string> dict;
		dict.insert({ "sort", "haha_rsort" });
		dict.insert({ "left", "haha_left" });
		dict.insert({ "right", "haha_right" });

		dict["left"] = "left,last";
		dict["insert"] = "insert_ahah";
		dict["string"];

		unordered_map<string, string>::iterator it = dict.begin();
		while (it != dict.end())
		{
			// 不能修改first，可以修改second
			//it->first += 'x';
			// it->second += 'x';

			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;
	}
}